package com.sc.nft.service.impl;

import com.sc.nft.dao.EquityPropsDao;
import com.sc.nft.dto.IdRequest;
import com.sc.nft.entity.EquityProps;
import com.sc.nft.entity.UserEquityProps;
import com.sc.nft.enums.UserEquityPropsGetTypeEnum;
import com.sc.nft.exception.GlobalException;
import com.sc.nft.feign.WxMiniRedeemCardClient;
import com.sc.nft.feign.WxMiniUserRedeemCardClient;
import com.sc.nft.req.redeemcard.UserRedeemCardPageReq;
import com.sc.nft.req.redeemcard.UserRedeemCardReceiveReq;
import com.sc.nft.req.redeemcard.UserRedeemCardRecordReq;
import com.sc.nft.service.UserEquityPropsService;
import com.sc.nft.service.WxMiniUserRedeemService;
import com.sc.nft.sup.PageResult;
import com.sc.nft.sup.Result;
import com.sc.nft.vo.redeemcard.RedeemCardDetailsVO;
import com.sc.nft.vo.redeemcard.UserRedeemCardDetailsVO;
import com.sc.nft.vo.redeemcard.UserRedeemCardPageVO;
import com.sc.nft.vo.redeemcard.UserRedeemCardRecordsPageVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.util.Objects;

/**
 * @author JYT
 * @create 2024/5/18 13:25
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class WxMiniUserRedeemServiceImpl implements WxMiniUserRedeemService {

    private final WxMiniUserRedeemCardClient wxMiniUserRedeemCardClient;
    private final UserEquityPropsService userEquityPropsService;
    private final EquityPropsDao equityPropsDao;
    private final WxMiniRedeemCardClient wxMiniRedeemCardClient;

    @Override
    public PageResult<UserRedeemCardPageVO> userPage(UserRedeemCardPageReq request) {
        return wxMiniUserRedeemCardClient.userPage(request);
    }

    @Override
    public Result<UserRedeemCardDetailsVO> details(IdRequest request) {
        return wxMiniUserRedeemCardClient.details(request);
    }


    @Override
    public PageResult<UserRedeemCardRecordsPageVO> records(UserRedeemCardRecordReq request) {
        return wxMiniUserRedeemCardClient.records(request);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void receive(UserRedeemCardReceiveReq receiveReq) throws GlobalException {
        IdRequest idRequest = new IdRequest();
        idRequest.setId(receiveReq.getRedeemCardId());
        RedeemCardDetailsVO cardDetailsVO = wxMiniRedeemCardClient.homeDetails(idRequest);
        if (Objects.isNull(cardDetailsVO)) {
            throw new GlobalException("兑换失败");
        }
        EquityProps equityProps = equityPropsDao.getById(cardDetailsVO.getEquityPropsId());
        if (Objects.isNull(equityProps)) {
            throw new GlobalException("兑换失败");
        }
        UserEquityProps userEquityProps = userEquityPropsService.addEquityPropsByUser(equityProps, receiveReq.getUserId(), UserEquityPropsGetTypeEnum.MINI_MALL_EXCHANGE, receiveReq.getNum(), cardDetailsVO.getTitle(),
                cardDetailsVO.getId(), BigDecimal.ZERO, UserEquityPropsGetTypeEnum.MINI_MALL_EXCHANGE, cardDetailsVO.getId());
        if (Objects.isNull(userEquityProps)) {
            throw new GlobalException("兑换失败");
        }
        Result result = wxMiniUserRedeemCardClient.receive(receiveReq);
        if (result.getCode() != 0) {
            throw new GlobalException("兑换失败");
        }
    }
}
